package com.simdo.g768.Adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.simdo.g768.Bean.DoRfBean;
import com.simdo.g768.Listener.CellClickListener;
import com.simdo.g768.R;

import java.util.List;

public class DoRfAdapter extends RecyclerView.Adapter<DoRfAdapter.ViewHolder> {

	private final List<DoRfBean> list;
	private final CellClickListener listener;

	public DoRfAdapter(List<DoRfBean> list, CellClickListener listener) {
		this.list = list;
		this.listener = listener;
	}

	@NonNull
	@Override
	public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
		View view = LayoutInflater.from(viewGroup.getContext())
				.inflate(R.layout.item_do_rf, viewGroup, false);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
		final int pos = position;
		DoRfBean bean = list.get(pos);

		viewHolder.tv_cell.setText(String.valueOf(bean.getCellId() + 1));
		viewHolder.tv_param.setText(bean.getParam());

		String str = "启动";
		String color = "#1f99fe";
		switch (bean.getDoState()) {
			case 0:
				str = "启动";
				color = "#1f99fe";
				break;
			case 1:
				str = "启动中";
				color = "#999999";
				break;
			case 2:
				str = "结束";
				color = "#ff0000";
				break;
			case 3:
				str = "结束中";
				color = "#999999";
				break;
		}
		viewHolder.tv_start.setText(str);
		viewHolder.tv_start.setTextColor(Color.parseColor(color));
		viewHolder.tv_start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				switch (bean.getDoState()){
					case 0:
					case 2:
						listener.onClick(pos, 0);
						break;
					case 1:
					case 3:
						break;
				}
			}
		});

		boolean isCheck = false;
		boolean isEnable = false;
		switch (bean.getRfState()) {
			case 0: //空闲，关闭、且不可点击
                break;
			case 1: //启动中，关闭、且不可点击
				isCheck = true;
                break;
			case 2: //关闭中，打开、且不可点击
                break;
			case 3: //已启动，关闭、且可点击
                isEnable = true;
				break;
			case 4: //已启动，打开、且可点击
				isCheck = true;
				isEnable = true;
				break;
		}

		viewHolder.switch_rf.setChecked(isCheck);
		viewHolder.switch_rf.setEnabled(isEnable);
		viewHolder.switch_rf.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				listener.onClick(pos, 1);
			}
		});
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public int getItemCount() {
		return list.size();
	}

	public void updateDoState(int pos, int state){
		list.get(pos).setDoState(state);
		notifyItemChanged(pos);
	}

	public void updateRFState(int pos, int state){
		list.get(pos).setRfState(state);
		notifyItemChanged(pos);
	}

	public static class ViewHolder extends RecyclerView.ViewHolder {
		TextView tv_cell, tv_param, tv_start;
		@SuppressLint("UseSwitchCompatOrMaterialCode")
		Switch switch_rf;

        public ViewHolder(View view) {
			super(view);
			tv_cell = view.findViewById(R.id.tv_cell);
			tv_param = view.findViewById(R.id.tv_param);
			tv_start = view.findViewById(R.id.tv_start);
			switch_rf = view.findViewById(R.id.switch_rf);
		}
	}
}
